package com.situ.day12;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Array;
import java.util.Arrays;

import org.junit.Test;

public class IODemo {

	@Test
	public void testFileReader() {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("a.");
			int ch1 = fileReader.read();
			System.out.println((char)ch1);
			int ch2 = fileReader.read();
			System.out.println((char)ch2);
			int ch3 = fileReader.read();
			System.out.println((char)ch3);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
			
		}
	}
	
	@Test
	public void testFileReader1() {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("a");
			//1.没有到达流的末尾，返回的是读取的字符个数
			//2.到达流的末尾，返回-1
			char[] buffer = new char[10];
			int length = 0;
			while ((length = fileReader.read(buffer)) != -1) {
				System.out.println("length:"+length);
				System.out.println(Arrays.toString(buffer));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testFileWrite() {
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			fileReader = new FileReader("a");
			fileWriter = new FileWriter("b");
			int length = 0;
			char[] buffer = new char[10];
			while ((length = fileReader.read(buffer)) != -1) {
				System.out.println("length:"+length);
				System.out.println(Arrays.toString(buffer));
				fileWriter.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void testFileInputOutputStreamCopy() {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			fileInputStream = new FileInputStream("bd.png");
			fileOutputStream = new FileOutputStream("bd_back.png");
			byte[] buffer = new byte[1024];
			int length = 0;
			while ((length = fileInputStream.read(buffer)) != -1) {
				fileOutputStream.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
}
